package org.suyafei.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import org.suyafei.controller.request.AccountLoginRequest;
import org.suyafei.controller.request.AccountRegisterRequest;
import org.suyafei.enums.BizCodeEnum;
import org.suyafei.service.AccountService;
import org.suyafei.service.FileService;
import org.suyafei.utils.JsonData;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 宿某人
 * @since 2021-12-06
 */
@RestController
@RequestMapping("/api/account/v1")
public class AccountController {

    @Autowired
    private FileService fileService;

    @Autowired
    private AccountService accountService;

    /**
     * 文件上传 最大默认1M
     * 文件格式、扩展名等
     * @param multipartFile
     * @return
     */
    @PostMapping("upload")
    public JsonData uploadUserImg(@RequestPart("file") MultipartFile multipartFile) {

        String result = fileService.uploadUserImg(multipartFile);

        return result != null ? JsonData.buildSuccess(result) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);

    }

    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping("register")
    public JsonData register(@RequestBody AccountRegisterRequest accountRegisterRequest) {
        JsonData jsonData = accountService.register(accountRegisterRequest);
        return jsonData;
    }

    @PostMapping("login")
    public JsonData login(@RequestBody AccountLoginRequest request) {

        JsonData jsonData = accountService.login(request);
        return jsonData;
    }

}

